<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_Form_Admin_Award_Create extends Engine_Form {
	public $_error = array ();
	
	public function init() {
		$user = Engine_Api::_ ()->user ()->getViewer ();
		$user_level = Engine_Api::_ ()->user ()->getViewer ()->level_id;
		
		$this->setTitle ( 'Post New Award' )->setDescription ( 'Compose your new award below, then click "Post Award" to save the award.' )->setAttrib ( 'name', 'badges_award_create' );
		
		$this->addElement ( 'Text', 'user_id', array ('label' => 'Member', 'description' => 'Enter Member ID or Email or Username', 'allowEmpty' => false, 'required' => true, 'filters' => array ('StringTrim' ), 'validators' => array (//  'Int',
		new Engine_Validate_Callback ( array ($this, 'validateMember' ) ) ) ) );
		
		$badgeOptions = Engine_Api::_ ()->badge ()->getBadgesAsSubOptions ( Badge_Model_Badge::MEMBERSHIP_TYPE_AWARD );
		$badgeOptions = array ("" => "" ) + $badgeOptions;
		$this->addElement ( 'Select', 'badge_id', array ('label' => 'Badge', 'description' => 'Select a badge that would be given to this member', 'multiOptions' => $badgeOptions, 'allowEmpty' => false, 'required' => true ) );
		
		$this->addElement ( 'Text', 'title', array ('label' => 'Title', 'description' => 'Enter a title for this award', 'allowEmpty' => false, 'required' => true, 'filters' => array ('StripTags', new Engine_Filter_Censor (), new Engine_Filter_StringLength ( array ('max' => '127' ) ) ) ) );
		
		// Description
		$this->addElement ( 'Textarea', 'description', array ('label' => 'Description', 'maxlength' => '255', 'filters' => array ('StripTags', //new Engine_Filter_Censor(),
		new Engine_Filter_StringLength ( array ('max' => '255' ) ) ) ) );
		
		$this->addElement ( 'TinyMce', 'body', array ('label' => 'Detailed Descriptions', 'filters' => array (new Engine_Filter_Censor () ), 'editorOptions' => array ('mode' => 'exact', 'elements' => 'body', 'width' => 450, 'height' => 260, 'plugins' => "emotions, table, fullscreen, media, preview, paste, style, layer, xhtmlxtras", 'theme_advanced_buttons1' => "cut,copy,paste,pastetext,pasteword,|,undo,redo,|,link,unlink,anchor,charmap,image,media,|,hr,removeformat,cleanup,code", 'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,|,bullist,numlist,|,outdent,indent,blockquote,|,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,preview", 'theme_advanced_buttons3' => "formatselect,fontselect,fontsizeselect,|,forecolor,backcolor,|,insertlayer,moveforward,movebackward,absolute", 'theme_advanced_buttons4' => "tablecontrols,|,styleprops,attribs,|,cite,del,ins" ) ) );
		
		/*
    // Element: start_time
    $this->addElement('CalendarDateTime', 'start_time', array(
      'label' => 'Start Date',
      'value' => date('M d Y'),
      'required' => true,
      'allowEmpty' => false,
    ));
		*/
		
		$this->addElement ( 'Text', 'rank', array ('label' => 'Order Ranking', 'description' => 'If above selected badge\'s members are sorted by "Order Ranking" on badge setting page, then enter position of this awarded member. All members who have this same badge will be sorted by Order Ranking. If the badge does not use this type of sorting, then just ignore this by entering 0 (zero).', 'value' => 0, 'filters' => array ('Int' ), 'validators' => array ('Int' ) ) );
		
		// Element: end_settings
		$this->addElement ( 'Radio', 'end_settings', array ('id' => 'end_settings', 'label' => 'Expiration Setting', 'onchange' => "updateTextFields(this)", 'multiOptions' => array ("0" => "Don't end this award on a specific date.", "1" => "End this award on a specific date." ), 'value' => 0 ) );
		
		// Element: end_time
		$this->addElement ( 'CalendarDateTime', 'end_time', array ('label' => 'End Date', 'value' => date ( 'M d Y' ), 'ignoreValid' => true ) );
		
		// View
		$availableLabels = array ('everyone' => 'Everyone', 'registered' => 'Registered Members', 'owner_network' => 'Friends and Networks', 'owner_member_member' => 'Friends of Friends', 'owner_member' => 'Friends Only', 'owner' => 'Just Me' );
		
		$options = $availableLabels;
		$this->addElement ( 'Select', 'auth_view', array ('label' => 'Privacy', 'description' => 'Who may see this award? (options are relative to awarded member)', 'multiOptions' => $options, 'value' => 'everyone' ) );
		$this->auth_view->getDecorator ( 'Description' )->setOption ( 'placement', 'prepend' );
		
		$options = $availableLabels;
		unset ( $options ['everyone'] );
		// Comment
		$this->addElement ( 'Select', 'auth_comment', array ('label' => 'Comment Privacy', 'description' => 'Who may post comments on this award? (options are relative to awarded member)', 'multiOptions' => $options, 'value' => 'registered' ) );
		$this->auth_comment->getDecorator ( 'Description' )->setOption ( 'placement', 'prepend' );
		
		$this->addElement ( 'Checkbox', 'featured', array ('label' => 'Mark this award as featured', 'value' => 0 ) );
		
		$this->addElement ( 'Checkbox', 'search', array ('label' => 'Show this award in browse page and search results', 'value' => 1 ) );
		
		// Buttons
		$this->addElement ( 'Button', 'submit', array ('label' => 'Post Award', 'type' => 'submit', 'ignore' => true, 'decorators' => array ('ViewHelper' ) ) );
		
		$this->addElement ( 'Cancel', 'cancel', array ('label' => 'cancel', 'link' => true, 'prependText' => ' or ', 'decorators' => array ('ViewHelper' ) ) );
		
		$this->addDisplayGroup ( array ('submit', 'cancel' ), 'buttons' );
		$button_group = $this->getDisplayGroup ( 'buttons' );
		$button_group->addDecorator ( 'DivDivDivWrapper' );
	
	}
	
	public function validateMember($value) {
		/*
    // Not string?
    if( !is_string($value) || empty($value) ) {
      $this->user->getValidator('Engine_Validate_Callback')->setMessage('Please complete this field - it is required.');
      return false;
    }
		*/
		
		$user = Engine_Api::_ ()->user ()->getUser ( $value );
		if (! $user->getIdentity ()) {
			$this->user_id->getValidator ( 'Engine_Validate_Callback' )->setMessage ( 'Could not find user %value%' );
			return false;
		}
		
		return true;
	}
}